/**
 * 
 */
package ac.svn.accesscontrol.ui.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.store.ISVNItem;
import ac.svn.accesscontrol.ui.STextField;
import ac.svn.accesscontrol.ui.model.ThreadedListModel;
import ac.svn.accesscontrol.ui.renderer.SimpleIconRenderer;
import ac.svn.accesscontrol.utils.UIUtility;
import ac.svn.accesscontrol.utils.Utility;

/**
 * @author Enigmanic
 *
 */
public class UserAccessEditorPanel extends JPanel {

	public interface ExportedComponents
	{
		public static final String C_UAR_PANEL								= "C_UAR_PANEL";
		public static final String C_UAR_ASSGND_LIST_VIEW 		= "C_UAR_ASSGND_LIST_VIEW";
		public static final String C_UAR_ASSGND_LIST_MODEL 	= "C_UAR_ASSGND_LIST_MODEL";
		public static final String C_UAR_AVL_LIST_VIEW 				= "C_UAR_AVL_LIST_VIEW";
		public static final String C_UAR_AVL_LIST_MODEL			= "C_UAR_AVL_LIST_MODEL";
		public static final String C_UAR_TXT_SEARCH_ASND		= "C_UAR_TXT_SEARCH_ASND";
		public static final String C_UAR_TXT_SEARCH_ALL			= "C_UAR_TXT_SEARCH_ALL";
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JList listAssigned,listAvailable;
	private ThreadedListModel avlListModel, assndListModel;
	private JCheckBox chckbxR, chckbxW;
	private JButton btnDelete, btnAdd,btnHelp;

	private STextField txtSearchAsndLoc;
	private JTextField txtSearchAllLoc;
	private JLabel lblGroups,lblUserName;
	private JScrollPane scrollPaneAvailable;
	
	public UserAccessEditorPanel()
	{
		super(true);
		setBackground(Color.WHITE);
		setLayout(new BorderLayout(0, 0));
		initComponents();
		//registerComponents();
	}

	private void initComponents()
	{
		Color WHITE = Color.WHITE;
		Border border = UIUtility.setAllShadowedBorder(null);

		//Set Background of panel itself.
		setBackground(WHITE);

		JPanel panelNorth = new JPanel();
		panelNorth.setBackground(WHITE);
		panelNorth.setPreferredSize(new Dimension(10, 40));
		panelNorth.setLayout(new BorderLayout(10, 1));
		panelNorth.setBorder(border);

		add(panelNorth, BorderLayout.NORTH);
		lblUserName = new JLabel(" User Name");
		lblUserName.setIcon(Utility.getImageIcon(IResources.LOC_ICN_USER));
		lblUserName.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblUserName.setHorizontalAlignment(SwingConstants.CENTER);
		panelNorth.add(lblUserName, BorderLayout.WEST);

		lblGroups = new JLabel("Group(s): 0, [g1, g2, ..]");
		panelNorth.add(lblGroups, BorderLayout.CENTER);

		JSplitPane splitPane = new JSplitPane();
		splitPane.setBackground(Color.WHITE);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setBorder(border);
		add(splitPane, BorderLayout.CENTER);

		JScrollPane scpnAssigned = new JScrollPane();
		scpnAssigned.setBackground(WHITE);
		splitPane.setLeftComponent(scpnAssigned);

		listAssigned = new JList();
		listAssigned.setBorder(border);
		assndListModel = new ThreadedListModel();
		listAssigned.setModel(assndListModel);
		SimpleIconRenderer iconRenderer = new SimpleIconRenderer(IResources.LOC_ICN_URL); 
		listAssigned.setCellRenderer(iconRenderer);
		iconRenderer.setDefBorder(new MatteBorder(0,2,1,0,Color.LIGHT_GRAY));
		scpnAssigned.setViewportView(listAssigned);

		JPanel panel = new JPanel();
		panel.setBackground(WHITE);
		panel.setBorder(border);
		panel.setPreferredSize(new Dimension(10, 45));
		panel.setLayout(new BorderLayout(2, 0));

		scpnAssigned.setColumnHeaderView(panel);


		txtSearchAsndLoc = new STextField("User has access to following locations, [Click here to search]");
		txtSearchAsndLoc.setEditable(false) ;

		panel.add(txtSearchAsndLoc,BorderLayout.CENTER);

		JPanel panelAssigndOps = new JPanel();
		panelAssigndOps.setBackground(WHITE);
		panelAssigndOps.setBorder(border);

		FlowLayout flowLayout = (FlowLayout) panelAssigndOps.getLayout();
		flowLayout.setHgap(0);
		flowLayout.setAlignment(FlowLayout.LEADING);
		flowLayout.setVgap(0);

		panel.add(panelAssigndOps, BorderLayout.EAST);

		chckbxR = new JCheckBox("R");
		chckbxR.setToolTipText("Add/Remove reading rights");
		chckbxR.setMnemonic('R');
		chckbxR.setBackground(WHITE);
		panelAssigndOps.add(chckbxR);

		chckbxW = new JCheckBox("W");
		chckbxW.setToolTipText("Add/Remove writing rights");
		chckbxW.setMnemonic('W');
		chckbxW.setBackground(WHITE);
		panelAssigndOps.add(chckbxW);

		btnDelete = new JButton("X");
		btnDelete.setToolTipText("Remove accessibility of location from user.");
		btnDelete.setMnemonic('X');
		panelAssigndOps.add(btnDelete);

		btnHelp = new JButton("?");
		panelAssigndOps.add(btnHelp);

		scrollPaneAvailable = new JScrollPane();
		scrollPaneAvailable.setBackground(WHITE);

		splitPane.setRightComponent(scrollPaneAvailable);

		listAvailable = new JList();
		listAvailable.setBorder(border);

		avlListModel = new ThreadedListModel();
		listAvailable.setModel(avlListModel);
		iconRenderer = new SimpleIconRenderer(IResources.LOC_ICN_URL);
		iconRenderer.setDefBorder(new MatteBorder(0,2,1,0,Color.LIGHT_GRAY));
		listAvailable.setCellRenderer(iconRenderer);
		scrollPaneAvailable.setViewportView(listAvailable);

		txtSearchAllLoc = new STextField("Choose location from below list.[Click here to search]");

		JPanel panelAvlOps = new JPanel();
		panelAvlOps.setBackground(WHITE);
		panelAvlOps.setBorder(border);
		panelAvlOps.setPreferredSize(new Dimension(10, 45));

		scrollPaneAvailable.setColumnHeaderView(panelAvlOps);
		panelAvlOps.setLayout(new BoxLayout(panelAvlOps, BoxLayout.X_AXIS));

		btnAdd = new JButton("+");
		btnAdd.setHorizontalAlignment(SwingConstants.LEADING);
		btnAdd.setToolTipText("Add location and access to user [ALT+A]");
		btnAdd.setMnemonic('A');

		panelAvlOps.add(txtSearchAllLoc);
		panelAvlOps.add(btnAdd);

		splitPane.setDividerLocation(0.5);
		splitPane.setDividerLocation(100);

		txtSearchAsndLoc.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				txtSearchAsndLoc.setEditable(true);				
			}
		});

		txtSearchAsndLoc.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent arg0) {
				txtSearchAsndLoc.setEditable(false);
				txtSearchAsndLoc.setText("User has access to following locations, [Click here to search]");
			}

			@Override
			public void focusGained(FocusEvent arg0) {
				txtSearchAsndLoc.setText("");		
			}
		});

		txtSearchAllLoc.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				txtSearchAllLoc.setEditable(true);				
			}
		});

		txtSearchAllLoc.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent arg0) {
				txtSearchAllLoc.setEditable(false);
				txtSearchAllLoc.setText("Choose location from below list.[Click here to search]");
			}

			@Override
			public void focusGained(FocusEvent arg0) {
				txtSearchAllLoc.setText("");		
			}
		});

		splitPane.addComponentListener(new ComponentAdapter() {

			@Override
			public void componentShown(ComponentEvent ce) {
				JSplitPane splitPane = (JSplitPane) ce.getSource();
				splitPane.setDividerLocation(0.5);				
			}

			@Override
			public void componentResized(ComponentEvent ce) {
				JSplitPane splitPane = (JSplitPane) ce.getSource();
				splitPane.setDividerLocation(0.5);				
				super.componentResized(ce);
			}		
		});		
	}

	public JList getListAssigned() {
		return listAssigned;
	}

	public JList getListAvailable() {
		return listAvailable;
	}

	public ThreadedListModel getAvlListModel() {
		return avlListModel;
	}

	public ThreadedListModel getAssndListModel() {
		return assndListModel;
	}

	public JCheckBox getChckbxR() {
		return chckbxR;
	}

	public JCheckBox getChckbxW() {
		return chckbxW;
	}

	public JButton getBtnDelete() {
		return btnDelete;
	}

	public JButton getButton() 
	{
		return btnAdd;
	}

	public void resetData()
	{
		assndListModel.clear();
		avlListModel.clear();
		lblGroups.setText("");
		lblUserName.setText("");

		SwingUtilities.invokeLater(new Runnable() 
		{
			@Override
			public void run() 
			{
				SwingUtilities.updateComponentTreeUI(listAssigned);
				SwingUtilities.updateComponentTreeUI(listAvailable);
			}
		});
	}

	public ISVNItem getSelectedAvailableLocation()
	{
		return (ISVNItem) listAvailable.getSelectedValue();
	}

	public ISVNItem getSelectedAssignedLocation()
	{
		return (ISVNItem) listAssigned.getSelectedValue();
	}

	public JButton getBtnAdd() {
		return btnAdd;
	}

	public JButton getBtnHelp() {
		return btnHelp;
	}

	public JLabel getLblGroupsg() {
		return lblGroups;
	}

	public JLabel getLblUserName() {
		return lblUserName;
	}

	/**
	 * @return the scrollPaneAvailable
	 */
	public JScrollPane getScrollPaneAvailable() {
		return scrollPaneAvailable;
	}

	/**
	 * @param scrollPaneAvailable the scrollPaneAvailable to set
	 */
	public void setScrollPaneAvailable(JScrollPane scrollPaneAvailable) {
		this.scrollPaneAvailable = scrollPaneAvailable;
	}


}
